package com.example.demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.R;
import com.example.demo.activity.CompActivity;
import com.example.demo.activity.FragmentMainActivity;
import com.example.demo.activity.H5Activity;
import com.example.demo.activity.NotificationActivity;

import java.util.List;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder> {

    private Context context;
    private List<ButtonItem> buttonItems;

    public GridAdapter(Context context, List<ButtonItem> buttonItems) {
        this.context = context;
        this.buttonItems = buttonItems;
    }

    @NonNull
    @Override
    public GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
        return new GridViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GridViewHolder holder, int position) {
        ButtonItem buttonItem = buttonItems.get(position);
        holder.button.setText(buttonItem.getName());

        holder.button.setOnClickListener(v -> {
            // 处理按钮点击事件，打开对应的URL
            if (buttonItem.getType() == 2) {
                // 如果 type 为 2，启动 H5Activity，并传递 URL


                Intent intent = new Intent(context, H5Activity.class);
                intent.putExtra("url", buttonItem.getUrl());  // 将 URL 传递给 H5Activity
                context.startActivity(intent);
            } else if (buttonItem.getType() == 3) {
                Intent intent = new Intent(context, CompActivity.class);
                context.startActivity(intent);
            } else if (buttonItem.getType() == 4) {
                Intent intent = new Intent(context, CompActivity.class);
                context.startActivity(intent);

            } else if (buttonItem.getType() == 5) {
                Intent intent = new Intent(context, NotificationActivity.class);
                context.startActivity(intent);
            }  else if (buttonItem.getType() == 6) {
            Intent intent = new Intent(context, FragmentMainActivity.class);
            context.startActivity(intent);
        } else {
                // 否则使用默认的浏览器打开 URL
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(buttonItem.getUrl()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return buttonItems.size();
    }

    public static class GridViewHolder extends RecyclerView.ViewHolder {
        Button button;

        public GridViewHolder(View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
        }
    }
}
